package com.spark.incoicings.conroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.*;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.spark_enum.SparkEnum;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/demand")
public class DemandController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private DemandService demandService;

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private ProductService productService;

    @Resource
    private ProcuredSupplierService supplierService;

    @Resource
    private EmpService empService;

    @Resource
    private OrderService orderService;

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private PurchaseDetailService purchaseDetailService;

    @Resource
    private DictDataService dictDataService;

    /**
     *删除采购订单
     * @return
     */
    @PostMapping("/delOrderById")
    public Result delOrderById(@RequestParam Integer id) {
        List<PurchaseDetail> list = purchaseDetailService.selectDetailById(id);
        if (list != null && list.isEmpty()) {
            if (orderService.removeById(id)) return Result.success();
        }
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), SparkEnum.DELETE_EXCEPTION.getMsg());
    }

    /**
     * 查询所有状态为带出库单采购订单产品
     * @return
     */
    @GetMapping("/selectPurchaseDetailStatusAll")
    public Result selectPurchaseDetailStatusAll() {
        List<PurchaseDetail> data = purchaseDetailService.selectPurchaseDetailStatusAll();
        return Result.success(200, "success", data);
    }

    /**
     * 根据id删除供应商
     * @return
     */
    @PostMapping("/delSupplierById")
    public Result delSupplierById(@RequestParam Integer id) {
        if(supplierService.delSupplierById(id)) return Result.success();
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), "删除失败! 该供应商下有采购订单 或 联系管理员!");
    }

    /**
     * 根据id更新供应商
     * @param supplier
     * @return
     */
    @PostMapping("/updateSupplierById")
    public Result updateSupplierById(@RequestBody ProcuredSupplier supplier) {
        if (supplierService.updateSupplierById(supplier)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 根据id获取供应商
     * @param id
     * @return
     */
    @GetMapping("/selectSupplierById")
    public Result selectSupplierById(@RequestParam Integer id) {
        ProcuredSupplier data = supplierService.selectSupplierById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @PostMapping("/addProcuredSupplier")
    public Result addProcuredSupplier(@RequestBody ProcuredSupplier supplier) {
        supplier.setDelayRate(0);
        return supplierService.addProcuredSupplier(supplier);
    }

    /**
     * 查询所有供应商
     * @param pageNum
     * @param pageSize
     * @param sName
     * @return
     */
    @GetMapping("/selectSupplierAll")
    public Result selectSupplierAll(@RequestParam(defaultValue = "1")
                                        Integer pageNum,
                                    @RequestParam(defaultValue = "10")
                                        Integer pageSize,
                                    @RequestParam(required = false)
                                        String sName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("sName", sName);
        List<ProcuredSupplier> list = supplierService.selectSupplierAll(map);
        PageInfo<ProcuredSupplier> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id更新采购明细
     * @param purchaseDetail
     * @return
     */
    @PostMapping("/updatePurchaseDetailById")
    public Result updatePurchaseDetailById(@RequestBody PurchaseDetail purchaseDetail) {
        if ( purchaseDetailService.updatePurchaseDetailById(purchaseDetail)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 添加采购明细
     * @param purchaseDetail
     * @return
     */
    @PostMapping("/addPurchaseDetail")
    public Result addPurchaseDetail(@RequestBody PurchaseDetail purchaseDetail) {
        return purchaseDetailService.addPurchaseDetail(purchaseDetail);
    }

    /**
     * 查询所有采购单号
     * @return
     */
    @GetMapping("/selectOrderNumberAll")
    public Result selectOrderNumberAll() {
        List<Order> data = orderService.list();
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有采购明细
     * 分页
     * 根据采购单号模糊查询
     * @param pageNum
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/selectPurchaseDetailAll")
    public Result selectPurchaseDetailAll(@RequestParam(defaultValue = "1")
                                          Integer pageNum,
                                          @RequestParam(defaultValue = "10")
                                          Integer pageSize,
                                          @RequestParam(required = false)
                                          String number) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("number", number);
        List<PurchaseDetail> list = purchaseDetailService.selectPurchaseDetailAll(map);
        PageInfo<PurchaseDetail> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id更新采购订单状态
     * @param order
     * @return
     */
    @PostMapping("/updateOrderByStatusId")
    public Result updateOrderByStatusId(@RequestBody Order order) {
        log.info("==============================>{}", order);
        if ( orderService.updateById(order)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 根据id更新采购订单和采购明细
     * @param order
     * @return
     */
    @PostMapping("/updateOrderById")
    public Result updateOrderById(@RequestBody Order order) {
        if (orderService.updateOrderById(order)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 查询所有产品和销售订单明细
     * @return
     */
    @GetMapping("/productAll")
    public Result productAll() {
        Map<String, Object> data = new HashMap<>();
        List<Product> productList = productService.list();
        List<OrderDetail> orderDetailList = orderDetailService.selectOrderDetails();
        data.put("productList", productList);
        data.put("orderDetailList", orderDetailList);
        return Result.success(200, "success", data);
    }

    /**
     * 查询订单明细
     * @return
     */
    @GetMapping("/selectOrderDetails")
    public Result selectOrderDetails() {
        List<OrderDetail> data = orderDetailService.selectOrderDetails();
        return Result.success(200, "success", data);
    }

    /**
     * 采购订单添加
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order) {
        return orderService.addOrder(order);
    }

    /**
     * 查询所有采购订单
     * 分页
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/selectOrderAll")
    public Result selectOrderAll(@RequestParam(defaultValue = "1")
                                 Integer pageNum,
                                 @RequestParam(defaultValue = "10")
                                 Integer pageSize,
                                 @RequestParam(required = false)
                                 Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("id", id);
        List<Order> list = orderService.selectOrderAll(map);
        PageInfo<Order> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id删除采购需求
     * @return
     */
    @PostMapping("/delDemandById")
    public Result delDemandById(@RequestParam Integer id) {
        if (demandService.removeById(id)) return Result.success();
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), SparkEnum.DELETE_EXCEPTION.getMsg());
    }

    /**
     *
     * 需求采购页面数据加载
     * 分页查询
     * 根据状态查询
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/selectDemandAll")
    public Result selectDemandAll( @RequestParam(defaultValue = "1")
                                   Integer pageNum,
                                   @RequestParam(defaultValue = "10")
                                   Integer pageSize,
                                   @RequestParam(required = false)
                                   Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("demandStatusId",id);
        List<Demand> list = demandService.selectDemandAll(map);
        PageInfo<Demand> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id获取采购需求
     * @param demandId
     * @return
     */
    @GetMapping("/selectDemandById")
    public Result selectDemandById(@RequestParam Integer demandId) {
        Demand data = demandService.selectDemandById(demandId);
        return Result.success(200, "success", data);
    }

    /**
     * 保存采购需求
     * @param demand
     * @return
     */
    @PostMapping("/addDemand")
    public Result addDemand(@RequestBody Demand demand) {
        return demandService.addDemand(demand);
    }

    /**
     * 根据id更新采购需求
     * @param demand
     * @return
     */
    @PostMapping("/updateDemand")
    public Result updateDemand(@RequestBody Demand demand) {
        if (demandService.updateDemandById(demand)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }


    /**
     * 查询字典数据通用方法
     * @param type
     * @return
     */
    @GetMapping("/selectDictDataByType")
    public Result selectDictDataByType(@RequestParam String type) {
        List<DictData> list = (List<DictData>) redisTemplate.opsForValue().get(type);
        if (list != null){
            return Result.success(200,"success",list);
        }
        List<DictData> shipment = dictDataService.selectDictDataByType(type);
        redisTemplate.opsForValue().set(type,shipment,1, TimeUnit.HOURS);
        return Result.success(200,"success",shipment);
    }

    /**
     * 查询采购需求状态和类型
     * @param type
     * @return
     */
    @GetMapping("/selectDictTypeByType")
    public Result selectDictTypeByType(@RequestParam String type) {
        List<DictType> data = dictTypeService.selectDictTypeByType(type);
        return Result.success(200, "success", data);
    }

    /**
     * 查询仓库
     * @return
     */
    @GetMapping("/warehouseAll")
    public Result warehouseAll() {
        List<Warehouse> data = warehouseService.list();
        return Result.success(200, "success", data);
    }

    /**
     * 根据仓库id获取所有商品
     * @param id
     * @return
     */
    @GetMapping("/productById")
    public Result productById(@RequestParam Integer id) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouse_id", id);
        List<Product> data = productService.list(wrapper);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有供应商
     * @return
     */
    @GetMapping("/supplierAll")
    public Result supplierAll() {
        List<ProcuredSupplier> data = supplierService.list();
        return Result.success(200, "success", data);
    }

    /**
     * 查询除超级管理员外所有用户
     * @return
     */
    @GetMapping("/empAll")
    public Result empAll() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.ne("employee_id", 1);
        List<Emp> data = empService.list(wrapper);
        return Result.success(200, "success", data);
    }

}
